package logintest.dao.impl;

import logintest.bean.User;
import logintest.dao.UserDao;
import logintest.util.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;

public class UserDaoImpl extends DBUtils implements UserDao {
    @Override
    public int Test(User user) {
        int update=0;
        ArrayList params = new ArrayList();
        try {
            String sql = "select * from logintest where uname=? and upass=? ";
            String uname = user.getUname();
            String upass = user.getUpass();
            params.add(uname);
            params.add(upass);
            resultSet = query(sql,params);
            while (resultSet.next()){
                if (resultSet!=null){
                    update++;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        if (update>0){
            return 1;
        }else return 0;
    }

    @Override
    public User UnameTest(String uname) {
        int update = 0;
        ArrayList params = new ArrayList();
        User user =new User();
        try {
            String sql = "select * from logintest where uname=?";
            params.add(uname);
            resultSet = query(sql,params);
            while (resultSet.next()){
                if (resultSet!=null){
                    user.setUid(resultSet.getString("uid"));
                    user.setMoney(resultSet.getString("money"));
                    update++;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        if (update>0){
            return user;
        }else return null;
    }
}
